package ips.abuse_attack.action;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import com.opensymphony.xwork2.ActionSupport;

public class AbuseSet extends ActionSupport {
	
	private int len;
	private int mod;
	private int stat;
	private int bytesize;

	
	private String rule;
	private String strrec;
	private String ip;
	private String address;
	
	DatagramSocket Socket ;
	
	public int getLen() {
		return len;
	}



	public void setLen(int len) {
		this.len = len;
	}



	public String getRule() {
		return rule;
	}



	public void setRule(String rule) {
		this.rule = rule;
	}



	public String getStrrec() {
		return strrec;
	}



	public void setStrrec(String strrec) {
		this.strrec = strrec;
	}



	public String getIp() {
		return ip;
	}



	public void setIp(String ip) {
		this.ip = ip;
	}



	public String getAddress() {
		return address;
	}



	public void setAddress(String address) {
		this.address = address;
	}



	public DatagramSocket getSocket() {
		return Socket;
	}

	public void setMod(int m) {
		mod = m;
	}

	public void setStat(int s) {
		stat = s;
	}

	public void setSocket(DatagramSocket socket) {
		Socket = socket;
	}



		
	/**
	 * 获取本机IP和名字
	 */
	public void getip(){
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			ip = addr.getHostAddress().toString();//获得本机IP
			address = addr.getHostName().toString();//获得本机名称
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		System.out.println("ip:"+ip);
		System.out.println("addr"+address);
	}
	/**
	 * 发送规则SOCKET
	 */
	public void send_rule() {


		try {

			setMod(5);
			setStat(2);
			len=12+rule.length();
			byte result[] = new byte[12+rule.length()];
			result[0] = (byte) mod;
			mod = mod >> 8;
			result[1] = (byte) mod;
			mod = mod >> 8;
			result[2] = (byte) mod;
			mod = mod >> 8;
			result[3] = (byte) mod;
			mod = mod >> 8;
			result[4] = (byte) stat;
			stat = stat >> 8;
			result[5] = (byte) stat;
			stat = stat >> 8;
			result[6] = (byte) stat;
			stat = stat >> 8;
			result[7] = (byte) stat;
			stat = stat >> 8;
			result[8] = (byte) len;
			len = len >> 8;
			result[9] = (byte) len;
			len = len >> 8;
			result[10] = (byte) len;
			len = len >> 8;
			result[11] = (byte) len;
			len = len >> 8;
			byte[] byteArray = rule.getBytes();
			for(int i=0;i<rule.length();i++)
			{
				result[12+i] = byteArray[i];
			}
			getip();
			if(Socket==null)
			{
			Socket= new DatagramSocket();
			}
			DatagramPacket dataPack = new DatagramPacket(
					result, 
					12+rule.length(), 
					InetAddress.getByName(ip), 
					new Integer(3000));
			try {
				Socket.send(dataPack);
			//	sendSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

	}
	/**
	 * 接收底层返回信息 判断操作是否成功
	 */
	public String receive() {
		//try {
		//	DatagramSocket rec = new DatagramSocket(3000);
			//Socket= new DatagramSocket();
		
			byte b [] = new byte[8];
			DatagramPacket dprec = new DatagramPacket(b, 8);
			try {
				Socket.receive(dprec);
				strrec = new String(dprec.getData(), 0, dprec.getLength());
				Socket.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
	//	}
		if ("success".equals(strrec)) {
			return "success";
		}
		else
		{
			return "failed";
		}
	}
	public String execute() throws Exception{
		
		send_rule();
		if("success".equals(receive()))
		{
			return "success";
		}
		else{
			return "failed";
		}
	}
}
